package com.liangzhong.modules.Main.Mediator
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.LobbyEvent;
	import com.liangzhong.events.MainEvent;
	import com.liangzhong.mediator.BaseMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.modules.user.mediator.UserMediator;
	import com.lz.football.module.main.view.IMainView;
	
	import flash.display.DisplayObject;
	
	import org.puremvc.as3.interfaces.INotification;
	
	/**
	 *主界面mediator 
	 * @author lxh
	 * 
	 */	
	public class MainMediator extends BaseMediator
	{
		public static const MediatorName :String = "MainMediator";
		
		public function MainMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get mainView():IMainView
		{
			return viewComponent as IMainView;
		}
		/**
		 * 初始化
		 * 
		 */		
		override public function initMediator():void
		{
			addEventListeners();
		}
		/**
		 * 
		 * 添加事件
		 */		
		override public function addEventListeners():void
		{
			AppEventDispatcher.getInstance().addEventListener(LobbyEvent.Lobby_EnterGameRoom,onEnterGameRoom);
			AppEventDispatcher.getInstance().addEventListener(LobbyEvent.Lobby_Sit_Down,onSitDown);
			AppEventDispatcher.getInstance().addEventListener(LobbyEvent.Lobby_Quit_Room,onQuitRoomH);
		}
		/**
		 *请求进入游戏房间 
		 * @param e
		 * {roomId:roomId,gameType:gameType}
		 */		
		private function onEnterGameRoom(e:LobbyEvent):void
		{
			this.sendNotification(ApplicationConstants.Lobby_enterGameRoom,e.param);
		}
		/**
		 * 坐下 
		 * @param e
		 * 
		 */		
		private function onSitDown(e :LobbyEvent):void
		{
			this.sendNotification(ApplicationConstants.Lobby_SitDown,e.param);
		}
		/**
		 *退出房间 
		 * @param e
		 * 
		 */		
		private function onQuitRoomH(e :LobbyEvent):void
		{
			this.sendNotification(ApplicationConstants.Lobby_leaveGameRoom);
		}
		/**
		 * 主界面显示正常
		 */		
		private function onMainShowNormalHandler(e :*):void
		{
			mainView.showNormalUI();
			//获取签到信息
			this.sendNotification(ApplicationConstants.Login_Get_Login_Award_Info);
			//请求好友列表
			this.sendNotification(ApplicationConstants.FRIEND_GETFRIENDLIST);
			//注册mediator
			registerModuleMediator();
		}
		/**
		 *显示巡回赛 
		 * @param e
		 */		
		private function onMainShowTourMatchHandler(e :*):void
		{
			mainView.showTourMatch();
		}
		/**
		 * 捕获
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case NotificationConstants.MAINVIEW_ADD_DISPLAYOBJECT://添加二级场景
				{
					mainView.addDisplayObj(notification.getBody().dispalyObj);
					mainView.type = notification.getBody().type;
					if(notification.getBody().dispalyObj)
					{
						mainView.closeTaskFadeIn();
						//mainView.unShownAnimate();
						GlobalDef.GameWindowContainer.visible = false;
					}
					break;
				}
				case NotificationConstants.MAINVIEW_REMOVE_DISPLAYOBJECT://移除二级场景
				{
					mainView.removeDisplayObj();
					//mainView.showAnimate();
					GlobalDef.GameWindowContainer.visible = true;
					break;
				}
				case NotificationConstants.MATCH_START://比赛开始
				{
					//mainView.unShowMainUI();
					//GlobalDef.GameContainer.mouseChildren = false;
					//GlobalDef.GameContainer.mouseEnabled = false;
					break;
				}
				case NotificationConstants.MATCH_END://比赛结束
				{
					//mainView.showMainUI();
					break;
				}
				case NotificationConstants.GUIDE_START://新手引导开始
				{
					mainView.unShowUpAndDownUI();
					AppData.getInstance().user.isGuideing = true;
					break;
				}
				case NotificationConstants.GUIDE_SET_FINISH://新手设置完成
				{
					mainView.showMainUI();
					mainView.guideSetFinish();
					break;
				}
				case NotificationConstants.GUIDE_FINISH://新手引导完成
				{
					mainView.showUpAndDownUI();
					mainView.showMainUI();
					AppData.getInstance().user.isGuideing = false;
					//获取签到信息
					//this.sendNotification(ApplicationConstants.Login_Get_SignInfo);
					//请求任务列表
					//this.sendNotification(ApplicationConstants.TASK_GETTASKLIST);
					//请求好友列表
					//this.sendNotification(ApplicationConstants.FRIEND_GETFRIENDLIST);
					//请求冲级活动信息
					//this.sendNotification(ApplicationConstants.Activity_Get_Grade_Info);
					//注册mediator
					registerModuleMediator();
					break;
				}
				case NotificationConstants.MAIN_SHOW_UP_DOWN://显示上下面板
				{
					mainView.showUpAndDownUI();
					break;
				}
				case NotificationConstants.MAIN_SHOW_ANIMATE://显示场景动画
				{
					mainView.showAnimate();
					break;
				}
				case NotificationConstants.MAIN_UNSHOW_ANIMATE://隐藏场景动画
				{
					mainView.unShownAnimate();
					break;
				}
				case NotificationConstants.MAIN_SET_CHATPANEL://设置聊天面板
				{
					mainView.setChatPanel(notification.getBody());
					break;
				}
				case NotificationConstants.MAIN_SHOW_CHRISTMAS://圣诞
				{
					mainView.showChristmasEf();
					break;
				}
			}
		}
		/**
		 * 监听
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [NotificationConstants.MAINVIEW_ADD_DISPLAYOBJECT,
					NotificationConstants.MAINVIEW_REMOVE_DISPLAYOBJECT,
					NotificationConstants.MATCH_START,
					NotificationConstants.MATCH_END,
					NotificationConstants.GUIDE_START,
					NotificationConstants.GUIDE_SET_FINISH,
					NotificationConstants.GUIDE_FINISH,
					NotificationConstants.MAIN_SHOW_ANIMATE,
					NotificationConstants.MAIN_UNSHOW_ANIMATE,
					NotificationConstants.MAIN_SHOW_UP_DOWN,
					NotificationConstants.MAIN_SET_CHATPANEL,
					NotificationConstants.MAIN_SHOW_CHRISTMAS];
		}
		/**
		 * 注册模块mediator
		 */		
		private function registerModuleMediator():void
		{
			facade.registerMediator(new UserMediator(UserMediator.MediatorName));//用户
		}
		/**
		 * 注册
		 * 
		 */		
		override public function onRegister():void
		{
			var mView :IMainView = Reflection.createInstance("com.city930.module.main.view.MainView");
			mView.initUI();
			viewComponent = mView;;
			GlobalDef.GameContainer.addChild(mView as DisplayObject);
			initMediator();
		}
		/**
		 *清除二次场景 
		 * @param e
		 * 
		 */
		private function onRemoveSecSceneHandler(e:MainEvent):void
		{
			var type :* = e.param;
			this.sendNotification(type);
			this.sendNotification(NotificationConstants.MAINVIEW_NOTICE_REMOVE_DISPLAYOBJECT);
		}
	}
}